<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\WeddingDress;
use App\Models\WeddingDressType;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;

class WeddingDressController extends Controller
{
    public function responseWeddingDressInfo()
    {
        $responseData = WeddingDress::get();
        return response()->json($responseData);
    }

    public function dropDownOption()
    {
        $dropDownOption = weddingDress::all('dressid', 'dressname', 'dressphoto', 'dressprice');
        $value = array("dressid" => 0, "dressname" => "选择需要预约的婚纱");
        $dropDownOption = $dropDownOption->toArray();
        array_unshift($dropDownOption, $value);
        return response()->json($dropDownOption);
    }

    public function showImagePrice(Request $req)
    {
        if ($req->dressid != 0) {
            $dropDownOption = weddingDress::where('dressid', $req->dressid)->get();
            return response()->json($dropDownOption);
        }
    }

    // web
    public function update(Request $req)
    {
        $updateDressInfo =  WeddingDress::where('dressid', $req->dressid)->get();
        return response()->json($updateDressInfo);
    }

    public function save(Request $req)
    {
        $updateDressInfo =  WeddingDress::find($req->dressid);
        $updateDressInfo->dresstheme = $req->dresstheme;
        $updateDressInfo->dressname = $req->dressname;
        $updateDressInfo->dressintroduce = $req->dressintroduce;
        $updateDressInfo->dressprice = $req->dressprice;
        $updateDressInfo->save();
        return response()->json([['code' => 200, 'msg' => '修改信息成功']]);
    }

    public function remove(Request $req)
    {
        $remove = WeddingDress::where('dressid', $req->dressid)->delete();
        if ($remove) {
            return response()->json([['code' => 200, 'msg' => '删除成功']]);
        } else {
            return response()->json([['code' => 201, 'msg' => '删除失败']]);
        }
    }

    public function add(Request $req)
    {
        $rule = [
            'dressname' => 'required',
            'dressphoto' => 'required|image',
            'dressintroduce' => 'required',
            'dresstheme' => 'required',
            'dressprice' => 'required',
            'typeid' => 'required'
        ];
        $dress = [
            'dressname' => $req->dressname,
            'dressphoto' => $req->dressimage,
            'dressintroduce' => $req->dressintroduce,
            'dresstheme' => $req->dresstheme,
            'dressprice' => $req->dressprice,
            'typeid' => $req->dresstype,
        ];
        // 验证用户输入是否正确（格式）
        $check = Validator::make($dress, $rule);
        if ($check->fails()) {
            dd('请将信息补充完整');
            return response()->json(['code' => 201, 'msg' => '请将信息补充完整']);
        } else {
            $file_name = $req->file('dressimage')->getClientOriginalName();
            $req->file('dressimage')->storeAs('images', $file_name, 'public/storage/images');
            $dress['dressphoto'] = $file_name;
            WeddingDress::create($dress);
            return redirect('admin/weddingdress');
        }
    }

    public function search(Request $req){
        $weddingDressType = WeddingDress::where('dressname','like', '%'.$req->keyword.'%')->get();
        return response()->json($weddingDressType);
    }
}
